// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ComponentBase from "../../../../general/Script/component/ComponentBase";
import DataMgr from "../../../../general/Script/mgr/dataManager";
import GameMgr from "../../../../general/Script/mgr/gameManager";
import { BlockState } from "../content";


const {ccclass, property} = cc._decorator;




@ccclass
export default class Block extends ComponentBase {

    
    blockDiamon:cc.Node = null;
    
    //方块颜色
    blockType:number = 0;

    //星星类型
    starType:number = 0;
    
    //方块状态
    state:number = 0;

    row:number = 0;
    col:number = 0;
   
    // bg:cc.Node = null;

    block:cc.Node = null;

    // rc:cc.Node = null;

    onLoad () {
       super.onLoad();
       
       GameMgr.Ins().on(`${this.name}_${this.node.uuid}`,'blockTouchMove',this.blockTouchMove.bind(this))
    }

    start () {
        
    }


    blockTouchMove(x,y){
        const x1 = this.node.x-40;
        const x2 = this.node.x+40;

        const y1 = this.node.y-40;
        const y2 = this.node.y+40;
        
    }
    init(row,col,state,type){
        this.row = row;
        this.col = col;
        this.changeState(state,type);
        this.node.name = row + '_' + col;
        // this.rc.getComponent(cc.Label).string = this.node.name;
    }

    setBlockType(type){
        const spArr = DataMgr.Ins().getGameData('blockSp');
        this.block.getComponent(cc.Sprite).spriteFrame = spArr[type];
        this.block.color = cc.color(255,255,255);
        this.blockType = type;
        if(this.block.childrenCount > 0){
            this.block.children[0].active = false;
        }
    }

    setBlockMidType(type){
        const spArr = DataMgr.Ins().getGameData('blockSp');
        this.block.color = cc.color(255,255,255);
        this.block.getComponent(cc.Sprite).spriteFrame = spArr[type];
        if(this.block.childrenCount > 0){
            this.block.children[0].active = false;
        }
    }
    setTip(){
        this.block.opacity = 100;

    }

    setNormal(){
        this.block.opacity = 255;
    }

    
    changeState(state,type){
        this.state = state;
        const diamondSp =  DataMgr.Ins().getGameData('blockDiamondSp');
        
        const stoneSp = DataMgr.Ins().getGameData('blockStoneSp');
        switch(state){
            case BlockState.none:
                this.block.active = false
                break
            case BlockState.mid:
                this.block.active = true;
                this.setBlockType(type);
                this.setTip();
                break
            case BlockState.show:
            case BlockState.showGame:
                this.block.active = true;
                this.setBlockType(type);
                this.setNormal();
                break;
            case BlockState.clear:
                this.setBlockMidType(type);
                break;
            case BlockState.over:
                const sp = DataMgr.Ins().getGameData('blockOverSp');
                this.block.getComponent(cc.Sprite).spriteFrame =sp;
                this.block.color = cc.color(100,100,100);
                break;
            case BlockState.stone:
                this.block.active = true
                this.setNormal();
                console.log('stone = ',this.row,this.col,state,type)
                this.block.getComponent(cc.Sprite).spriteFrame = stoneSp;
                this.state = BlockState.stone;
                break;
            case BlockState.star:
                this.block.active = true
                this.setNormal();
                console.log('star = ',this.row,this.col,state,type)
                this.block.getComponent(cc.Sprite).spriteFrame = diamondSp;
                this.setDiamondType(type);
                this.state = BlockState.star;
                break;


        }
    }


    setDiamondType(type){
        const diamondSpArr = DataMgr.Ins().getGameData('diamondsSp');
        console.log('this.block.childrenCount  = ',this.block.childrenCount );
        console.log()
        if(this.block.childrenCount > 0){
            this.block.children[0].getComponent(cc.Sprite).spriteFrame = diamondSpArr[type];
            this.block.children[0].active = true;
        }else{
            const node = new cc.Node();
            node.addComponent(cc.Sprite).spriteFrame = diamondSpArr[type];
            node.parent = this.block;
            node.active = true;
        }
        this.starType = type;
    }


    // update (dt) {}
}
